package com.google.gwt.indexeddb.client;

import com.google.gwt.core.client.JavaScriptException;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArrayString;

/**
 * @author Chan Le <gwt /at/ chanvn.com>
 * @See <a href= "http://www.w3.org/TR/IndexedDB/#database-interface" >database- interface</a>
 */
public class IDBDatabase extends JavaScriptObject {
  protected IDBDatabase() {
  }

  /**
   * Returns immediately and closes the connection in a separate thread. The connection is not
   * actually closed until all transactions created using this connection are complete. No new
   * transactions can be created for this connection once this method is called. Methods that create
   * transactions throw an exception if a closing operation is pending.
   * 
   * @see <a href = "http://www.w3.org/TR/IndexedDB/#widl-IDBDatabase-close">W3C
   *      IDBDatabase-close</a>
   */
  public final native void close() /*-{
		this.close();
  }-*/;

  /**
   * Creates and returns a new object store with the given name in the connected database. This
   * method must be called only from a VERSION_CHANGE transaction callback.
   * 
   * @param name The name of the new object store.
   * @param keyPath The key path to be used by the new object store. If empty or not specified, the
   *          object store is created without a key path and using out-of-line keys.
   * @param autoIncrement If true, the object store will have a key generator. Defaults to false.
   * @see <a href="http://www.w3.org/TR/IndexedDB/#widl-IDBDatabase-createObjectStore">W3C
   *      IDBDatabase-createObjectStore</a>
   */
  public final IDBObjectStore createObjectStore(String name, String keyPath, boolean autoIncrement)
      throws IDBDatabaseException {
    try {
      return createObjectStore0(name, keyPath, autoIncrement);
    } catch (JavaScriptException e) {
      throw new IDBDatabaseException(e.getName(), e);
    }
  }

  private final native IDBObjectStore createObjectStore0(String name, String keyPath,
      boolean autoIncrement)/*-{
		this.createObjectStore(name, keyPath, autoIncrement);
  }-*/;

  /**
   * Destroys the object store with the given name in the connected database, along with any indexes
   * that reference it. Note that this method must only be called from a VERSION_CHANGE transaction
   * callback.
   * 
   * @param name The name of an existing object store to delete.
   * @return IDBRequest A request object one which subsequent events related to this operation are
   *         fired.
   * @throws IDBDatabaseException This method can raise an IDBDatabaseException with the following
   *           codes: NOT_ALLOWED_ERR If the method was not called from a VERSION_CHANGE transaction
   *           callback. NOT_FOUND_ERR If there is no object store with the given name (based on a
   *           case-sensitive comparison) in the connected database.
   * @see <a href="http://www.w3.org/TR/IndexedDB/#widl-IDBDatabase-deleteObjectStore">W3C
   *      IDBDatabase-deleteObjectStore</a>
   */
  public final IDBRequest deleteObjectStore(String name) throws IDBDatabaseException {
    try {
      return deleteObjectStore0(name);
    } catch (JavaScriptException e) {
      throw new IDBDatabaseException(e.getName(), e);
    }
  }

  private final native IDBRequest deleteObjectStore0(String name) /*-{
		return this.deleteObjectStore(name);
  }-*/;

  /**
   * @return The name of the connected database.
   * @see <a href="http://www.w3.org/TR/IndexedDB/#widl-IDBDatabase-name">W3C IDBDatabase-name </a>
   */
  public final native String getName() /*-{
		return this.name;
  }-*/;

  /**
   * @return The version of the connected database. When a database is first created, this attribute
   *         is the empty string.
   * @see <a href= "http://www.w3.org/TR/IndexedDB/#widl-IDBDatabase-version>W3 C
   *      IDBDatabase-version</a>
   */
  public final native String getVersion() /*-{
		return this.version;
  }-*/;

  /**
   * *
   * 
   * @return A list of the names of the object stores currently in the connected database
   * @see <a href="http://www.w3.org/TR/IndexedDB/#widl-IDBDatabase-objectStoreNames">W3C
   *      IDBDatabase-objectStoreNames</a>
   */
  public final native JsArrayString getObjectStoreNames()/*-{
		return this.objectStoreNames;
  }-*/;

  /**
   * Returns immediately and runs a VERSION_CHANGE transaction on the connected database in a
   * separate thread.
   * 
   * @param name The version to store in the connected database.
   * @return {@link IDBVersionChangeRequest} A request object on which subsequent events related to
   *         this operation are fired.
   * @see <a href="http://www.w3.org/TR/IndexedDB/#widl-IDBDatabase-setVersion">W3C
   *      IDBDatabase-setVersion</a>
   */
  public final IDBVersionChangeRequest setVersion(String version) throws IDBDatabaseException {
    try {
      return setVersion0(version);
    } catch (JavaScriptException e) {
      throw new IDBDatabaseException(e.getName(), e);
    }
  }

  private final native IDBVersionChangeRequest setVersion0(String version0) /*-{
		return this.setVersion(version0);
  }-*/;

  /**
   * Immediately returns an IDBTransaction object, and starts a transaction in a separate thread. If
   * starting the transaction takes longer than the specified timeout interval (for example because
   * other READ_WRITE transactions on the some of the same objects stores are not finished), then it
   * is aborted, and an error event, with a code of TIMEOUT_ERR is fired against any requests that
   * have been placed against the transaction and that are not done.
   * 
   * @param storeNames A list of names of object stores and indexes in the scope of the new
   *          transaction. If this parameter is unspecified, empty, or null, then the scope of the
   *          transaction is the entire connected database.
   * @param mode The mode for isolating access to data in the specified object stores. If not
   *          specified, this parameter defaults to READ_ONLY. Possible values are the
   *          IDBTransaction mode constants.
   * @param timeout The interval, in milliseconds, that this operation is allowed to take to reserve
   *          all the database objects in this transaction's scope.
   * @return {@link IDBTransaction} A transaction object created by the method.
   * @throws IDBDatabaseException
   * @see <a href="">W3C </a>
   */
  public final IDBTransaction transaction(JsArrayString storeNames, int mode)
      throws IDBDatabaseException {
    try {
      return transaction0(storeNames, mode);
    } catch (JavaScriptException e) {
      throw new IDBDatabaseException(e.getName(), e);
    }
  }

  private final native IDBTransaction transaction0(JsArrayString storeNames, int mode) /*-{
    return this.transaction(storeNames, mode); 
  }-*/;
}
